package main

import (
	"fmt"
	. "nowcoder/common"
)

func removeNthFromEnd(head *ListNode, n int) *ListNode {
	// write code here
	node := head
	l := 0
	for node != nil {
		l++
		node = node.Next
	}
	fmt.Println(l)

	p1, p2 := head, head

	d := l - n + 1

	for d > 0 {
		p2 = p2.Next
		d--
	}

	for p2 != nil {
		p1 = p1.Next
		p2 = p2.Next
	}

	fmt.Println(p1.Val)
	return nil

}

func main() {
	head := &ListNode{
		Val: 1,
		Next: &ListNode{
			Val:  2,
			Next: nil,
		},
	}

	removeNthFromEnd(head, 2)
}
